/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.tools {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.util.MLBitmapUtil;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.media.Video;
	import flash.net.URLRequest;

	/**
	 * @author Marek Brun
	 */
	public class FLVScreenGrabber extends MLBroadcastClass implements IMLBroadcasterListener {

		public var event_ScreenGrabbed:String='ScreenGrabbed';
		
		public var vo:Video;
		private var player:FLVPlayer;
		
		public function FLVScreenGrabber(flvURL:URLRequest, width:Number, height:Number) {
			this.vo=new Video(width, height);
			vo.visible=false;
			player=new FLVPlayer(vo);
			player.setupFileURL(flvURL);
			player.playingProgress.addListener(this);
			player.play();
			player.volume.v=0;
		}

//********************************************************************************************
//		events for FLVScreenGrabber
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case player.playingProgress:
					switch(ev.event) {
						case player.playingProgress.event_Changed:
							if(player.getGotMetaData() && player.getTime()>2){
								player.playingProgress.removeListener(this);
								broadcastEvent(event_ScreenGrabbed, MLBitmapUtil.getBitmapDataByDisplay(vo));
								player.kill();
								player=null;
							}
						break;
					}
				break;
			}
		}
		
	}
}
